package annotation;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-07-12
 * Time:17:44
 */
/*
Servlet的URL匹配
1.精确匹配  "/hiServlet/a" -> url = "http://localhost:8080/Servlet/hiServlet/a" 请求该Servlet
2.目录匹配  "/hiServlet/*" -> url = "http://localhost:8080/Servlet/hiServlet/a" |
                                    "http://localhost:8080/Servlet/hiServlet/bb/ccc" ...
                                    *后目录下任意URL路径都可请求该Servlet
3.扩展名匹配 "*.action" -> url = "http://localhost:8080/Servlet/abc.action" |
                                "http://localhost:8080/Servlet/defg.action" ...
                                *代表任意字串,符合*.action的URL路径都可请求该Servlet
                                注意:该匹配方式的urlPattern不能带'/'
4.任意匹配  "/*" | "/" -> url = "http://localhost:8080/Servlet/hiServlet/a" |
                                "http://localhost:8080/Servlet/hiServlet/bb/ccc" |
                                "http://localhost:8080/Servlet/abc.action" ...
                                任意URL路径都可请求该Servlet
                                注意:该匹配方法会拦截静态资源请求(Tomcat的DefaultServlet),尽量不用
多个Servlet的不同URL匹配优先级
    精确匹配 > 目录匹配 > 扩展名匹配 > "/*" > "/"
 */
@WebServlet(urlPatterns = "/hiServlet", initParams = {@WebInitParam(name = "aa",value = "A"),@WebInitParam(name = "bb",value = "B")})
public class HiServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Annotation doGet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Annotation doPost");
    }
}
